package com.three.iot.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.three.iot.api.SysResourceService;
import com.three.iot.commons.dto.SysResourceCondDto;
import com.three.iot.commons.dto.SysResourceCreateDto;
import com.three.iot.commons.dto.SysResourceModiDto;
import com.three.iot.commons.dto.SysResourceResultDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.jdbc.model.SysResource;
import com.three.iot.jdbc.mapper.SysResourceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:luiz
 * @Date: 2018/3/9 10:21
 * @Descripton:
 * @Modify :
 **/
@Service
public class SysResourceServiceImpl implements SysResourceService {
    @Resource
    private SysResourceMapper sysResourceMapper;

    @Override
    public Result<String> create(SysResourceCreateDto sysResourceCreateDto) {
        SysResource oldSysResource = sysResourceMapper.selectBySourceNo(sysResourceCreateDto.getSourceNo());
        if (oldSysResource != null) {
            return Result.newError(ResultCode.COMMON_DATA_EXISTS);
        }
        SysResource sysResource = new SysResource();
        BeanUtils.copyProperties(sysResourceCreateDto, sysResource);
        sysResource.setSortOrder(Integer.valueOf(sysResourceCreateDto.getSortOrder()));
        sysResource.setpSourceNo(sysResourceCreateDto.getPsourceNo());
        sysResource.setSourceLevel(Integer.valueOf(sysResourceCreateDto.getSourceLevel()));
        sysResource.setSourceIcon(sysResourceCreateDto.getSourceIcon());
        sysResource.setCreateTime(new Date());
        sysResource.setCreateNo(sysResourceCreateDto.getUserNo());
        sysResourceMapper.insertSelective(sysResource);
        return Result.newSuccess();
    }

    public Result update(SysResourceModiDto sysResourceModiDto) {
        SysResource oldResource = sysResourceMapper.selectByPrimaryKey(Integer.valueOf(sysResourceModiDto.getId()));
        if (oldResource == null) {
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        oldResource.setSortOrder(Integer.valueOf(sysResourceModiDto.getSortOrder()));
        oldResource.setpSourceNo(sysResourceModiDto.getSourceNo());
        oldResource.setSourceLevel(Integer.valueOf(sysResourceModiDto.getSourceLevel()));
        oldResource.setSourceIcon(sysResourceModiDto.getSourceIcon());
        oldResource.setModiTime(new Date());
        oldResource.setModiNo(sysResourceModiDto.getUserNo());
        sysResourceMapper.updateByPrimaryKeySelective(oldResource);
        return Result.newSuccess();
    }

    public Result delete(String id) {
        sysResourceMapper.deleteByPrimaryKey(Integer.valueOf(id));
        return Result.newSuccess();
    }

    public Result<SysResource> findById(String id) {
        return Result.newSuccess(sysResourceMapper.selectByPrimaryKey(Integer.valueOf(id)));
    }

    public Result<List<SysResourceResultDto>> getListByCurrentUser(String currentUserNo) {
        List<SysResource> sysResourceList = sysResourceMapper.selectByUserNo(currentUserNo);
        return Result.newSuccess(buldDto(sysResourceList));
    }

    private List<SysResourceResultDto> buldDto(List<SysResource> sysResourceList) {
        List<SysResourceResultDto> sysResourceResultDtos = new ArrayList<SysResourceResultDto>();
        for (SysResource sysResource : sysResourceList) {
            sysResourceResultDtos.add(buildSingleDto(sysResource));
        }
        return sysResourceResultDtos;
    }

    private SysResourceResultDto buildSingleDto(SysResource sysResource) {
        SysResourceResultDto sysResourceResultDto = new SysResourceResultDto();
        sysResourceResultDto.setId(String.valueOf(sysResource.getSourceNo()));
        sysResourceResultDto.setName(sysResource.getSourceName());
        sysResourceResultDto.setRoute(sysResource.getReqUrl());
        sysResourceResultDto.setId(String.valueOf(sysResource.getSourceNo()));
        sysResourceResultDto.setSourceId(String.valueOf(sysResource.getId()));
        sysResourceResultDto.setSourceNo(sysResource.getSourceNo());
        sysResourceResultDto.setName(sysResource.getSourceName());
        sysResourceResultDto.setRoute(sysResource.getReqUrl());
        sysResourceResultDto.setSortOrder(sysResource.getSortOrder().toString());
        sysResourceResultDto.setSourceType(sysResource.getSourceType());
        sysResourceResultDto.setIfVisible(sysResource.getIfVisible());
        sysResourceResultDto.setSourceLevel(sysResource.getSourceLevel().toString());
        if (sysResource.getSourceLevel() == 1) {
            sysResourceResultDto.setpId(null);
            sysResourceResultDto.setBpid(null);
            sysResourceResultDto.setMpid(null);
        } else {
            sysResourceResultDto.setpId(sysResource.getpSourceNo());
            sysResourceResultDto.setBpid(sysResource.getpSourceNo());
            sysResourceResultDto.setMpid(sysResource.getpSourceNo());
        }

        sysResourceResultDto.setIcon(sysResource.getSourceIcon());
        sysResourceResultDto.setSourceIcon(sysResource.getSourceIcon());
        return sysResourceResultDto;
    }

    public Page<SysResource> listPage(SysResourceCondDto sysResourceCondDto) {
        //使用PageHelper类进行分页
        PageHelper.startPage(sysResourceCondDto.getPageNum(), sysResourceCondDto.getPageSize());
        Page<SysResource> sysResourcePage = sysResourceMapper.selectList(sysResourceCondDto);
        return sysResourcePage;
    }

    public Result<List<SysResourceResultDto>> findReSourceLike(String condStr) {
        List<SysResource> sysResourceList = sysResourceMapper.selectByCond(condStr);
        return Result.newSuccess(buldDto(sysResourceList));
    }

    public Result<List<SysResourceResultDto>> listAll() {
        List<SysResource> sysResourceList = sysResourceMapper.selectAll();
        return Result.newSuccess(buldDto(sysResourceList));
    }


}
